﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BackgroundCommand.cs" company="">
//   
// </copyright>
// <summary>
//   The background command.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace F5.BigIp.LocalTrafficManager.UI.Commands
{
    using System;
    using System.ComponentModel;
    using System.Windows.Input;

    /// <summary>The background command.</summary>
    public abstract class BackgroundCommand : ICommand
    {
        /// <summary>The background worker.</summary>
        private readonly BackgroundWorker backgroundWorker;

        /// <summary>Initializes a new instance of the <see cref="BackgroundCommand"/> class.</summary>
        protected BackgroundCommand()
        {
            this.backgroundWorker = new BackgroundWorker();

            this.backgroundWorker.DoWork += this.DoWork;

            this.backgroundWorker.RunWorkerCompleted += this.RunWorkerCompleted;
        }

        /// <summary>The can execute changed.</summary>
        public event EventHandler CanExecuteChanged;
        ////add { CommandManager.RequerySuggested += value; }
        ////remove { CommandManager.RequerySuggested -= value; }

        /// <summary>The do work.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        protected abstract void DoWork(object sender, DoWorkEventArgs e);

        /// <summary>The run worker completed.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        protected abstract void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e);

        /// <summary>The can execute.</summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        public virtual bool CanExecute(object parameter)
        {
            return this.backgroundWorker.IsBusy == false;
        }

        /// <summary>The execute.</summary>
        /// <param name="parameter">The parameter.</param>
        public virtual void Execute(object parameter)
        {
            this.backgroundWorker.RunWorkerAsync();
        }
    }
}